<?php

class data_widget
{
    private $dir = '';

    public function __construct() {
        $this->dir = EXPORT_PATH;
    }

    public function export_images($topic_name = '') {

        if (empty($topic_name) || !in_array($topic_name, topic_config::$TOPIC)) {
            return false;
        }

        $dir = $this->dir . 'topic_' . $topic_name;

        $filter = array(
            '1' => 1,
            'status' => 1,
        );

        if (array_key_exists($topic_name, topic_config::$term)) {
            $filter['parent_id'] = explode(',', topic_config::$term[$topic_name]);
        }

        $photo_list = _model('topic_' . $topic_name)->getList($filter, 'ORDER BY upload_time DESC');

        if (!empty($photo_list)) {
            if (!is_dir($dir)) {
                mkdir($dir);
            }
            foreach ($photo_list as $item) {
                $url = _image($item['src']);
                $extname = substr($url, strrpos($url, '.'));
                if ($extname == '.') {
                    $extname = '.jpg';
                }
                $store_info = _uri('store', $item['store_id']);
                $file_name = $store_info['title'] . '-' . $store_info['title_branch'] . $extname;

                if ($url && !file_exists($dir . '/' . $file_name)) {
                    $res = $this->get_image($url);
                    if ($res['code'] === 200) {
                        file_put_contents($dir . '/' . $file_name, $res['content']);
                    }
                }
            }

            return true;
        }

        return false;
    }

    private function get_image($url) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_URL, $url);
        ob_start();
        curl_exec($ch);
        $return_content = ob_get_contents();
        ob_end_clean();
        $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $return_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
        curl_close($ch);
        return array('code' => $return_code, 'content' => $return_content, 'content_type' => $return_type);
    }

    public function download($topic_name = '') {

        if (empty($topic_name) || !in_array($topic_name, topic_config::$TOPIC)) {
            return false;
        }

        $dir = $this->dir . 'topic_' . $topic_name;

        $zipFile = $dir . '.zip';
        if (file_exists($zipFile)) {
            Response::redirect(STATIC_URL . '/images/data/topic_' . $topic_name . '.zip');
        } elseif (is_dir($dir)) {
            $zip = new ZipArchive();
            if ($zip->open($zipFile, ZipArchive::CREATE) === TRUE) {
                $handler = opendir($dir); //打开当前文件夹由$path指定。
                while (($filename = readdir($handler)) !== false) {
                    if ($filename != "." && $filename != "..") {//文件夹文件名字为'.'和‘..’，不要对他们进行操作
                        if (file_exists($dir . "/" . $filename)) {
                            $zip->addFile($dir . "/" . $filename, 'topic_' . $topic_name . '/' . $filename);
                        }
                    }
                }
                @closedir($dir);
            }

            $zip->close(); //关闭处理的zip文件

            header("Cache-Control: public");
            header("Content-Description: File Transfer");
            header('Content-disposition: attachment; filename=' . basename($zipFile)); //文件名
            header("Content-Type: application/zip"); //zip格式的
            header("Content-Transfer-Encoding: binary"); //告诉浏览器，这是二进制文件
            header('Content-Length: ' . filesize($zipFile)); //告诉浏览器，文件大小
            @readfile($zipFile);
            @unlink($zipFile);

            return '打包完成再次点击下载';
        }
    }
}

?>